package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //员工登录
    @PostMapping("/login")
    private R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1.将页面提交的密码进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据页面提供的用户名查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        //3.如果没有查到,则返回登录失败的结果
        if (emp == null) {
            return R.error("登录失败");
        }

        //4.比对密码
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }

        //5.查看员工状态,如果为已禁用,则返回员工已被禁用
        if (emp.getStatus() == 0) {
            return R.error("用户已被禁用");
        }

        //6.登录成功,将对象id放入Session中,并返回登录成功的结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }


    //员工退出
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("成功退出");
    }

    //新增员工
    @PostMapping
    public R save(HttpServletRequest request,@RequestBody Employee employee){
        //设置默认数据
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置默认时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //获取操作者id
        //Long empId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);

        employeeService.save(employee);
        return R.success("修改成功");
    }

    //员工分页查询
    @GetMapping("/page")
    public R page(int page, int pageSize,String name){
        //构造分页构造器
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        //构造条件过滤器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加过滤条件
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //按更新时间降序
        lqw.orderByDesc(Employee::getUpdateTime);

        //执行查询,将结果封装到pageInfo中
        employeeService.page(pageInfo,lqw);

        //返回pageInfo
        return R.success(pageInfo);
    }

    //修改员工信息
    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Employee employee){
        //修改时间
        employee.setUpdateTime(LocalDateTime.now());
        //修改者
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(empId);

        long id = Thread.currentThread().getId();
        log.info("线程id为:{}",id);

        employeeService.updateById(employee);

        return R.success("修改成功");
    }

    //获取员工id
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        Employee emp = employeeService.getById(id);
        if (emp != null){
            return R.success(emp);
        }
        return R.error("没有该员工");
    }
}
